package cn.edu.sctu.sctu_lysd.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.bean.JsonBaseListBean;
import cn.edu.sctu.sctu_lysd.bean.ParcelData;
import cn.edu.sctu.sctu_lysd.biz.IndexBiz;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.net.NetService;
import cn.edu.sctu.sctu_lysd.net.RetrofitHelper;
import cn.edu.sctu.sctu_lysd.utils.AnimationHelper;
import cn.edu.sctu.sctu_lysd.utils.JumpUtils;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.utils.LightUtils;
import cn.edu.sctu.sctu_lysd.utils.ScreenUtils;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;
import cn.edu.sctu.sctu_lysd.view.act.IndexActivity;
import cn.edu.sctu.sctu_lysd.view.act.IndexPlacePopWin;
import cn.edu.sctu.sctu_lysd.view.act.LoginAct;
import cn.edu.sctu.sctu_lysd.view.act.SubmitReleaseAct;
import cn.edu.sctu.sctu_lysd.view.act.TouristMessageAct;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangfuxin on 2017-04-06.
 */

public class IndexPresenter implements IndexBiz.Presenter {

    private Context mContext;
    private SharePreferenceUtils mSharePreference;
    private Activity mActivity;

    private IndexPlacePopWin placePopWin;

    private NetService service;
    private Call<JsonBaseListBean<ParcelData>> pacelCall;


    public IndexPresenter(Context mContext, IndexBiz.IndexBizView mView) {
        this.mContext = mContext;
        this.mView = mView;

        mActivity = (Activity) mContext;
    }

    private IndexBiz.IndexBizView mView;


    @Override
    public void start() {

        mSharePreference = SharePreferenceUtils.getInstance(mContext);

        RetrofitHelper helper = RetrofitHelper.getInstance();

        service = (NetService) helper.getService(NetService.class);

    }

    @Override
    public void isLogin() {

        if (mSharePreference.ifHaveShare(CacheKeyConfig.CACHE_ACCESS_TOKEN)) {

            mView.alreadyLogin();

        } else {

            mView.notLogin();
        }

    }

    @Override
    public void intentToLogin() {
        Intent intent = new Intent(mContext, LoginAct.class);

        mContext.startActivity(intent);
    }

    @Override
    public void showPlacePupWin(final View view) {

        LightUtils.lightOff(mActivity);

        View contentView = LayoutInflater.from(mContext).inflate(R.layout.pup_win_place, null);

        placePopWin = new IndexPlacePopWin(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true,
                mContext, view);

        ImageView back = (ImageView) contentView.findViewById(R.id.back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                placePopWin.dismiss();
            }
        });

        placePopWin.setSelectOver(new IndexPlacePopWin.selectOver() {
            @Override
            public void selectOver(String place) {

                /**
                 * 通知 View 显示
                 */
                mView.selectPlaceOver(place);

                placePopWin.dismiss();

            }
        });

        placePopWin.showAsDropDown();

        placePopWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                LightUtils.lightOn(mActivity);
            }
        });

    }

    @Override
    public void showDemandWin(final View view) {

        LightUtils.lightOff(mActivity);

        view.startAnimation(AnimationHelper.getInstance().getRotateAnimation(0, 90, 200, 0));

        View contentView = LayoutInflater.from(mContext).inflate(R.layout.pup_win_demand, null);

        final PopupWindow demandPopupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        demandPopupWindow.setTouchable(true);
        demandPopupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        demandPopupWindow.setWidth(ScreenUtils.getScreenWidth(mContext));
        demandPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);

        ColorDrawable dw = new ColorDrawable(0xff0000);
        demandPopupWindow.setBackgroundDrawable(dw);
        demandPopupWindow.showAsDropDown(view, 0, 0);
        demandPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                view.startAnimation(AnimationHelper.getInstance().getRotateAnimation(90, 360, 200, 0));
                LightUtils.lightOn(mActivity);
            }
        });

        Button btn1 = (Button) contentView.findViewById(R.id.demand_btn1);
        Button btn2 = (Button) contentView.findViewById(R.id.demand_btn2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();

                bundle.putString(IndexActivity.ORIGIN_PRE, mView.getOriginPre());
                bundle.putString(IndexActivity.ORIGIN_DETAIL, mView.getOriginDetail());

                JumpUtils.jumpActivity(mContext, SubmitReleaseAct.class, bundle);
                demandPopupWindow.dismiss();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();

                bundle.putString(IndexActivity.ORIGIN_PRE, mView.getOriginPre());
                bundle.putString(IndexActivity.ORIGIN_DETAIL, mView.getOriginDetail());

                JumpUtils.jumpActivity(mContext, TouristMessageAct.class, bundle);

                demandPopupWindow.dismiss();
            }
        });


    }

    @Override
    public void getParcels(final int page, int size) {

        pacelCall = service.getAllParcels(page, size);

        pacelCall.enqueue(new Callback<JsonBaseListBean<ParcelData>>() {
            @Override
            public void onResponse(Call<JsonBaseListBean<ParcelData>> call, Response<JsonBaseListBean<ParcelData>> response) {

                if (response.code() == 200 && response.body().getMeta().getCode() == 200) {

                    mView.getParcelsSuccess(response.body().getData(), page);

                } else {

                    mView.getParcelsFailed(page);

                }

            }

            @Override
            public void onFailure(Call<JsonBaseListBean<ParcelData>> call, Throwable t) {

                mView.getParcelsFailed(page);

            }
        });


    }

    @Override
    public void logOut() {
        mSharePreference.clearCache();
    }

}
